import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta:{
      isNav:true
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫
var vm = new Vue();
router.beforeEach((to,from,next)=>{
  var auth = vm.$cookie.get("loginAuth");
  console.log(auth);
  if(to.path=="/login"){
    if(auth){
      router.push("/home")
    }else{
      next();
    }
  }else{
    if(auth){
      next();
    }else{
      router.push("/login")
    }
  }
})

export default router
